package com.ceres.farm.service.impl;

import com.ceres.farm.dao.CustFarmlandDeviceMapper;
import com.ceres.farm.pojo.CustFarmlandCrops;
import com.ceres.farm.pojo.CustFarmlandDevice;
import com.ceres.farm.service.CustFarmlandDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class CustFarmlandDeviceServiceImpl implements CustFarmlandDeviceService {
    @Autowired
    CustFarmlandDeviceMapper custFarmlandDeviceMapper;
    /**
     * 设备的安装
     * @param custFarmlandDevice
     */
    @Override
    @Transactional
    public void addDevice(CustFarmlandDevice custFarmlandDevice) {
            if (StringUtils.isEmpty(custFarmlandDevice.getMstage())){
                custFarmlandDevice.setMstage("0");
            }
            if(StringUtils.isEmpty(custFarmlandDevice.getDstage())){
                custFarmlandDevice.setDstage("0");
            }
        int insert = custFarmlandDeviceMapper.insertDevice(custFarmlandDevice);
            //修改设备表设备的安装状态
        Integer integer = custFarmlandDeviceMapper.updateDevice(custFarmlandDevice.getCdid());
        if(insert==0||integer==0){
            throw new RuntimeException("设备安装失败");
        }

    }

    /**
     * 设备的启动/停止
     * @param quota
     * @param device
     * @return
     */
    @Override
    public Integer autoStart(Integer quota, Integer device) {
        //根据设备ID查询农作物信息
      CustFarmlandCrops crops=  custFarmlandDeviceMapper.findCrops(device);
      if(crops==null){
          return 0;
      }
        //根据设备ID查询设备检测的指标
      Integer indexName=  custFarmlandDeviceMapper.findIndexName(device);
        if(indexName==null){
            return 0;
        }
        //根据农作物查询农作物的数据指标
           String index= custFarmlandDeviceMapper.finIndex(crops.getCname(),crops.getStage(),indexName);
        if(index==null){
            return 0;
        }

           //清除数据中的单位变为纯数字
        String[] split = index.split("-");
        //根据设备ID查询指标指标单位
           String unitIndex= custFarmlandDeviceMapper.findUnit(device);
        if(unitIndex==null){
            return 0;
        }
        //  遍历数据数组，进行替换
        //创建集合用于存放替换后的数据
        List<String> unList=new ArrayList<>();
        for (String s : split) {
            unList.add(s.replace(unitIndex,""));
        }
        //把字符串数组转换int数组
            //遍历字符数组

        List<Integer> list=new ArrayList<>();
        for (String s : unList) {
            list.add(Integer.parseInt(s));
        }
        //根据设备传入的数据判断是否开启设备
        int[] arr=new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            arr[i]=list.get(i);
        }
          //判断指标是否在范围内
        if(quota>arr[0]&&quota<arr[1]||quota<arr[0]&&quota>arr[1]){
            //传入指标在健康范围内
            //修改设备运行状态（停止）
            custFarmlandDeviceMapper.updateMstageStop(device);
            return 8000;
        }else
        {
            //传入指标不在范围内
            //修改设备运行状态（启动）
            custFarmlandDeviceMapper.updateMstageStart(device);
            return 8001;
        }

    }
    //手动管理设备
    @Override
    public void manualControl(Integer device, Integer mstage) {
       Integer integer= custFarmlandDeviceMapper.updateMstageManual(device,mstage);
       if(integer==0){
           throw new RuntimeException("状态修改失败");
       }
    }
}
